﻿var url = getUrlId().split('=');
var id = url[0];
var date = url[1].split('#')[0].split('-');
var y = parseInt(date[0], 10);
var m = parseInt(date[1], 10);
// console.log(y, m)

// document.getElementById('nav').innerHTML = template('navs', navinfo4);

$('.indexl43').addClass('active');

$.ajax({
  url: '/api/CommonApi?API=GetTeacherMonthApplyDetails',
  type: 'get',
  data: {
    TeacherMonthApplyID: url[0]
  },
  dataType: 'json',
  success: function (data) {
    if (data.Success) {
      console.log(data)
      // console.log(JSON.stringify(data))
      var datas = {};
      datas.data = data.ApiParamObj;
      // 开始时间
      datas.s = new Date(datas.data[0].ApplyDate).pattern('yyyy-MM-dd');
      // 结束时间
      datas.e = new Date(datas.data[datas.data.length - 1].ApplyDate).pattern('yyyy-MM-dd');
      // console.log(datas.data[13].ApplyDate, datas.data[0].ApplyDate)
      // 差旅天数
      // datas.sDays = getDaysBetween(datas.data[datas.data.length - 1].ApplyDate, datas.data[0].ApplyDate);
      datas.sDays = getDaysBetween(datas.s, datas.e);
      datas.sDays = datas.sDays === 0 ? 1 : datas.sDays;
      // 执行天数
      // datas.eDays = getDaysBetween(datas.data[datas.data.length - 1].ApplyDate, datas.data[0].ApplyDate);
      datas.eDays = getDaysBetween(datas.s, datas.e);
      datas.eDays = datas.eDays === 0 ? 1 : datas.eDays;
      var arr = {};
      var o = datas.data;
      var i = 0;
      var l = o.length;
      var type = '';
      for (; i < l; i++) {
        type = o[i].CostType.split('-')[0];
        // console.log(o[i])
        if (!arr[type]) {
          arr[type] = [];
        }
        arr[type].push(o[i].CostMoney);
      }
      // 生成费用名称对应的a-z的字母
      var keys = {};
      i = 0;
      var cost = 0;
      datas.allCost = 0;
      for (var key in arr) {
        cost = (function () {
          var i = 0;
          var l = arr[key].length;
          var c = 0;
          // console.log(arr[key])
          for (;i < l;i++) {
            c += arr[key][i];
          }
          return c;
        }());
        // console.log(cost)
        keys[getAToZ()[i]] = {
          name: getAToZ()[i],
          value: key,
          cost: cost
        };
        i++;

        datas.allCost += cost;
      }
      i = 0;
      datas.keys = keys;
      // console.log(datas.keys);

      document.getElementById('print').innerHTML = template('printTable', datas)
    }
  }
});

function getDaysBetween(s, e) {
  var start = new Date(s).getTime();
  var end = new Date(e).getTime();
  var c;
  if (start < end ) {
    c = (Math.floor((end - start) / (24 * 3600 * 1000)));
  } else {
    c = (Math.floor((start - end) / (24 * 3600 * 1000)));
  }
  return c;
}

/*
(function () {
  var data = [{
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "城市间交通费用-自驾",
    "CostExplain": "自驾说明-12",
    "CostMoney": 1
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "市内交通费用-地铁",
    "CostExplain": "地铁说明-12",
    "CostMoney": 2
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "住宿",
    "CostExplain": "住宿说明-2",
    "CostMoney": 3
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "包干",
    "CostExplain": "包干说明-2",
    "CostMoney": 4
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "项目",
    "CostExplain": "项目说明-2",
    "CostMoney": 5
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "城市间交通费用-自驾",
    "CostExplain": "自驾说明-12",
    "CostMoney": 6
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "市内交通费用-地铁",
    "CostExplain": "地铁说明-12",
    "CostMoney": 7
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "住宿",
    "CostExplain": "住宿说明-2",
    "CostMoney": 8
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "包干",
    "CostExplain": "包干说明-2",
    "CostMoney": 9
  }, {
    "ProjectName": "奔驰项目20160614",
    "UserName": "许素波",
    "ApplyDate": "2016/6/14 0:00:00",
    "CostType": "项目",
    "CostExplain": "项目说明-2",
    "CostMoney": 10
  }];

  var arr = {};
  var o = data;
  var i = 0;
  var l = o.length;
  var type = '';
  for (; i < l; i++) {
    type = o[i].CostType.split('-')[0];
    // console.log(o[i])
    if (!arr[type]) {
      arr[type] = [];
    }
    arr[type].push(o[i].CostMoney);
  }
  // 生成费用名称对应的a-z的字母
  var keys = {};
  i = 0;
  var cost = 0;
  for (var key in arr) {
    cost = (function () {
      var i = 0;
      var l = arr[key].length;
      var c = 0;
      // console.log(arr[key])
      for (;i < l;i++) {
        c += arr[key][i];
      }
      return c;
    }());
    console.log(cost)
    keys[getAToZ()[i]] = {
      name: getAToZ()[i],
      value: key,
      cost: cost
    };
    i++;
  }
  i = 0;
  console.log(keys)
}());*/
